package com.hightrast.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hightrast.service.api.domain.bo.QueryShopifyOrders;
import com.hightrast.service.api.entity.RastOrderProductItems;
import com.hightrast.service.api.entity.RastOrderProductRefundItems;
import com.hightrast.service.api.entity.RastOrders;
import com.hightrast.service.compareToData.EntitySynchronizer;
import com.hightrast.service.compareToData.stage.impl.RastOrderComparator;
import com.hightrast.service.compareToData.stage.impl.RastOrderUpdater;
import com.hightrast.service.converToClassMapper.OrderMapper;
import com.hightrast.service.mapper.RastOrdersMapper;
import com.hightrast.service.service.IRastOrderProductItemsService;
import com.hightrast.service.service.IRastOrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author hx
 * @since 2025-01-24
 */
@Service
@Slf4j
public class RastOrdersServiceImpl extends ServiceImpl<RastOrdersMapper, RastOrders> implements IRastOrdersService {


    @Resource
    IRastOrderProductItemsService orderProductItemsService;



    @Override
    @Transactional
    public void addOrders(List<RastOrders> orderList) {
        if(CollectionUtils.isEmpty(orderList)){
            log.info("新增订单数据为空");
            return;
        }
        List<RastOrders> oldOrders = this.list();
        //对比数据更新订单
        EntitySynchronizer.syncEntities(orderList,
                oldOrders,
                RastOrders::getId,
                this::updateBatchById,
                this::saveBatch,
                this::removeByIds,
                new RastOrderComparator(),
                new RastOrderUpdater());
        List<RastOrderProductItems> orderProductItems = orderList.stream().flatMap(order -> order.getLineItems().stream()).collect(Collectors.toList());
        List<RastOrderProductRefundItems> refundItems = orderList.stream().flatMap(refund -> refund.getRefundItems().stream()).collect(Collectors.toList());
        orderProductItemsService.addOrderProductItems(orderProductItems,refundItems);
    }

    @Override
    public List<RastOrders> addQo(List<QueryShopifyOrders> orderList) {
        List<RastOrders> rastOrders = orderList.stream()
                .peek(order -> {
                    List<QueryShopifyOrders.discountCodes> codes = CollectionUtil.isEmpty(order.getDiscount_codes()) ? ListUtil.empty() : order.getDiscount_codes();
                    QueryShopifyOrders.discountCodes discountCodes = codes.stream().findFirst().orElse(null);
                    if(CollectionUtil.isNotEmpty(order.getLine_items())){
                        order.getLine_items().forEach(item -> {
                            item.setOrder_id(order.getId());
                            if(Objects.nonNull(discountCodes)){
                                item.setTotal_discount(discountCodes.getAmount());
                                item.setDiscountType(discountCodes.getType());
                            }
                        });
                    }
                })
                .map(OrderMapper.INSTANCE::queryShopifyToOrder)
                .collect(Collectors.toList());
        return rastOrders;
    }
}
